package com.colin.paymentservice12001.service.impl;

import com.colin.common.bean.Order;
import com.colin.common.response.R;
import com.colin.common.status.OrderStatus;
import com.colin.paymentservice12001.feign.OrderFeign;
import com.colin.paymentservice12001.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {

//    @Autowired
//    private PaymentMapper paymentMapper;

    @Autowired
    private OrderFeign orderFeign;

    @Override
    public R<Object> doPay(String orderNum) {

        R<Order> rOrder = orderFeign.getOrder(orderNum);
        Order order = rOrder.getData();
        if (order == null) {
            return R.error();
        }

        if (OrderStatus.PAID.equals(order.getOrderStatus()) || OrderStatus.CLOSED.equals(order.getOrderStatus())) {
            return R.error();
        }

        R<Boolean> booleanR = orderFeign.changeOrderStatus(orderNum, OrderStatus.PAID);

        if (!booleanR.getData()) {
            return R.error();
        }

        log.info("订单号 : {} 正在支付中 ......", orderNum);
        log.info("订单号 : {} 支付完成", orderNum);
        return R.ok(true);
    }
}
